/**
 * Created by wangguoqing on 2017/11/8.
 */
var returnData;

var now = new Date();
var nowYear = now.getYear();
nowYear += (nowYear < 2000) ? 1900 : 0;
$(function () {
    resizeWindow();
    $("#username").focus();
    $("#username").focusout(function () {
        var username = $(this).val();
        if (username == '') {
            layer.tips('用户名不能为空', '#username');
        }
    });
    $("#password").focusout(function () {
        var password = $(this).val();
        if (password == '') {
            layer.tips('密码不能为空', '#password');
        }
    });
    //为keyListener方法注册按键事件
    document.onkeydown = function (e) {
        var theEvent = window.event || e;
        var code = theEvent.keyCode || theEvent.which;
        if (code == 13) {
            serverLogin();
        }
    }
    $( window ).resize(function() {
        resizeWindow();
    });

    // var s = "Copyright&nbsp;©" + nowYear + "&nbsp;TimeSoft.&nbsp;All Rights Reserved.";
    // document.getElementById("copyrightInfo").innerHTML = s;
});

function resizeWindow(){
    //var width = $(window).width();
    var height = $(window).height();
    var margin_height = (height - 630) / 2;
    $("#div_login").css('margin', ((margin_height < 10) ? 10:margin_height) + 'px' + ' auto');
}

//登录验证
function serverLogin() {
    var username = $("#username");
    var password = $("#password");
    if (username.val() == "") {
        layer.tips('用户名不能为空', '#username');
        return false;
    }
    if (password.val() == "") {
        layer.tips('密码不能为空', '#password');
        return false;
    }
    var userid = $("#username").val();
    var userpwd = $("#password").val();
    var url = path + "/system/register/login";
    var params = new Object();
    params.username = userid;
    params.password = userpwd;
    $.ajax({
        type: "POST",
        url: url,
        data: params,
        dataType: "json",
        success: function callback(data) {
            if (data.code == "0000") {
                returnData = data.returnObject;
                $("#orgCode").combobox({
                    editable:false,
                    disabled:false,
                    data:returnData,
                    valueField:'orgCode',
                    textField:'orgName',
                    onLoadSuccess:function(){
                    }
                });
                $("#orgCode").combobox('select', data.returnObject[0].orgCode);
                openOrg();
            } else if (data.code == "0002") {
                $("#orgIdCode").val(data.returnObject.orgCode);
                $("#orgName").val(data.returnObject.orgName);
                $("#login_form").submit();
            } else {
                $.messager.alert('错误', data.msg, 'error');
            }
        },
        error: function (data, textstatus) {
            $.messager.alert('错误', data.responseText, 'error', function () {
            });
        }
    });
}
function openOrg() {
    $("#organization").show();
    $("#organization").dialog({
        iconCls: "icon-man",
        width: 400,
        height: 135,
        modal: true,
        shadow: true,
        closed: false,
        resizable: false,
        closable: false,
        draggable: true,
        collapsible: false,
        minimizable: false,
        maximizable: false,
        buttons: [{
            text: "确定",
            iconCls: "icon-ok",
            handler: function () {
                var orgSelected = $("#orgCode").combobox("getValue");
                $("#orgIdCode").val(orgSelected);
                var orgName = $("#orgCode").combobox("getText");
                $("#orgName").val(orgName);
                if (orgSelected == "") {
                    layer.alert("请选择一个机构", {closeBtn: 0, shift: 4});
                } else {
                    $("#login_form").submit();
                }
            }
        }]
    });
}

